﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BrokerBase.Lib.Policy;
using BrokerBase.Lib.Person;

namespace Broker.View.PolicyView
{
    /// <summary>
    /// FastShcemeUserControl.xaml 的交互逻辑
    /// </summary>
    public partial class FastShcemeHead :SchemeHeadUserControl
    {
        private Scheme2Form schemeForm;

        public FastShcemeHead(IScheme scheme,Scheme2Form schemeForm):base(scheme)
        {
            InitializeComponent();
            this.schemeForm = schemeForm;
            Loaded += new RoutedEventHandler(FastShcemeUserControl_Loaded);
            schemeForm.SchemeChanged += new SchemeChangedHandler(OnScheme_Chagned);
        }

        private void SchemeChanged()
        {
            IPolicy t = Scheme.Policies.FirstOrDefault(p => p.Product != null);
            if (Scheme.Policies.Count > 0 && Scheme.Policies.FirstOrDefault(p => p.Product != null) != null)
            {
                rbgHolderSex.IsEnabled = false;
                rbgInsurantSex.IsEnabled = false;
                txtHolderAge.IsEnabled = false;
                txtInsurantAge.IsEnabled = false;
                sldHolderAge.IsEnabled = false;
                sldInsurantAge.IsEnabled = false;
            }
            else
            {
                rbgHolderSex.IsEnabled = true;
                rbgInsurantSex.IsEnabled = true;
                txtHolderAge.IsEnabled = true;
                txtInsurantAge.IsEnabled = true;
                sldHolderAge.IsEnabled = true;
                sldInsurantAge.IsEnabled = true;
            }
        }

        public override void OnScheme_Chagned()
        {
            SchemeChanged();
        }

        void FastShcemeUserControl_Loaded(object sender, RoutedEventArgs e)
        {
           
            if (LibGlobal.GetClientCfg().DefaultSex == "男")
            {
                rbtInsurantMale.IsChecked = true;
                rbtHolderMale.IsChecked = true;
            }
            else
            {
                rbtInsurantFmale.IsChecked = true;
                rbtHolderFmale.IsChecked = true;
            }
        }

        private void TextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Return)
            {
                TextBox txt = sender as TextBox;

                if (txt == txtInsurantAge)
                {
                    sldInsurantAge.Focus();
                }
                else if (txt == txtHolderAge)
                {
                    sldHolderAge.Focus();
                }
            }
        }

        private void sldHolderAge_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            RaisePersonChanged(PersonType.Holder,this.Scheme);
        }

        private void sldInsurantAge_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            RaisePersonChanged(PersonType.Insurant, this.Scheme);
        }


        private void rbtSex_Click(object sender, RoutedEventArgs e)
        {
            RaisePersonChanged(PersonType.Holder, this.Scheme);
        } 
    }
}
